package com.sunsharing.flow.model;

import com.sunsharing.flow.event.TransitionEvent;
import com.sunsharing.flow.exe.ExecutionContext;
import org.apache.log4j.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: criss
 * Date: 13-5-4
 * Time: 下午5:34
 * To change this template use File | Settings | File Templates.
 */
public class Transition extends GraphElement {

    /**日志**/
    transient private Logger logger = Logger.getLogger(this.getClass());

    //来源节点
    protected Node fromNode = null;

    //表达式
    String expression = "";

    //前进节点
    protected Node toNode = null;



    //是否允许退回
    protected boolean isBack = false;


    //支持的事件类型
    public static final Class[] supportedEventTypes = new Class[] {
            TransitionEvent.class
    };



    public Class[] getSupportedEventTypes() {
        return supportedEventTypes;
    }


    public Transition() {
    }


    public Node getFromNode() {
        return fromNode;
    }


    public void setFromNode(Node fromNode) {
        this.fromNode = fromNode;
    }


    public void setToNode(Node toNode) {
        this.toNode = toNode;
    }




    public Node getToNode() {
        return toNode;
    }




    /**
     * 功能描述:
     *     进入迁移
     * @param executeContext ExecutionContext
     */
    public void take(ExecutionContext executeContext) {

        logger.debug("进入迁移:" + this);
        //executeContext.setNode(null);
        executeContext.setTransition(this);

        //节点离开事件
        TransitionEvent event = new TransitionEvent(executeContext);
        //设置事件源
        event.setEventSource(this);
        multicastEvent(event);

        toNode.enter(executeContext);
    }

    /**
     * 功能描述:
     *     是否为回退迁移
     * @return boolean
     */
    public boolean isBack() {
        return isBack;
    }

    public void setBack(boolean isBack) {
        this.isBack = isBack;
    }

    public String toString() {
//        return ToStringBuilder.reflectionToString(this,
//                ToStringStyle.MULTI_LINE_STYLE, false, this.getClass());
        return this.id;
    }

    public String getExpression() {
        return expression;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }
}
